<?php

class SalesDetailsManager extends BaseManager implements iManager {

	public function query() {
		$results = array();

		$condition = $this->getCondition();

		$query = "SELECT
					sales_id,
					sales_detail_id,
					sd.product_id AS product_id,
					product_name,
					product_count,
					product_price,
					current_count,
					current_price
				FROM `sales_detail_tbl` AS sd
				LEFT JOIN `product_tbl` AS p
				ON p.product_id = sd.product_id";

		if (!empty($condition)) {
			$query .= $condition;
		}

		$this->db->query($query);

		$this->db->execute();

		while ($result = $this->db->getResultAssoc()) {
			$salesDetails = new SalesDetail_Domain();
				
			$salesDetails->setSalesDetailId($result['sales_detail_id']);
			$salesDetails->setProductQty($result['product_count']);
			$salesDetails->setTotalPrice($result['product_price']);
				
			$product = new Product_Domain();

			$product->setProductId($result['product_id']);
			$product->setProductName($result['product_name']);
			$product->setProductQty($result['current_count']);
			$product->setProductPrice($result['current_price']);
				
			$salesDetails->setProduct($product);

			$results[] = $salesDetails;
		}

		$this->result = $results;
	}

	public function add(){
		$sales_id = $this->obj->getSalesId();

		foreach ($this->obj->getSalesDetails() AS $salesDetails) {
			$data['product_id'] = $salesDetails->getProduct()->getProductId();
			$data['product_price'] = $salesDetails->getTotalPrice();
			$data['product_count'] = $salesDetails->getProductQty();
			$data['sales_id'] = $sales_id;

			$this->db->insert('sales_detail_tbl', $data);

			$this->db->execute();
		}
	}

	public function update(){
	}

	public function delete(){
	}
}